package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
	@Autowired
	private SetmealService setmealService;
	@Autowired
	private SetmealDishService setmealDishService;

	@Autowired
	private CategoryService categoryService;
	/**
	 * 新增套餐，同时需要保存套餐和菜品的关联关系
	 * @param setmealDto
	 */
	@Transactional
	public void saveWithDish(SetmealDto setmealDto) {
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Setmeal::getName,setmealDto.getName());
		Setmeal setmeal = setmealService.getOne(queryWrapper);
		if (setmeal!=null){
			throw new CustomException(setmealDto.getName()+"的套餐重名");
		}
		super.save(setmealDto);
		List<SetmealDish> dishList = setmealDto.getSetmealDishes().stream().map((setmealDish) -> {
			setmealDish.setSetmealId(setmealDto.getId());
			return setmealDish;
		}
		).collect(Collectors.toList());

		setmealDishService.saveBatch(dishList);

	}
	@Override
	public Page<SetmealDto> pageSetmealDto(Integer page, Integer pageSize, String name){
		//创建两个分页构造器
		Page<Setmeal> setmealPage = new Page<>(page,pageSize);
		Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
		//查询套餐Setmeal分页
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
		//只查询起售的套餐
		queryWrapper.eq(Setmeal::getStatus,1);
		BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

		List<SetmealDto> setmealDtoList = setmealPage.getRecords().stream().map(setmeal -> {
			SetmealDto setmealDto = new SetmealDto();
			BeanUtils.copyProperties(setmeal,setmealDto);

			Category category = categoryService.getById(setmeal.getCategoryId());
			if (category!=null){
				setmealDto.setCategoryName(category.getName());
			}
			return setmealDto;
		}).collect(Collectors.toList());

		setmealDtoPage.setRecords(setmealDtoList);
		return setmealDtoPage;
	}

	@Override
	public void removeWithDish(List<Long> ids) {
		//select count(*) from setmeal where id in (1,2,3) and status = 1
		//查询套餐状态，确定是否可用删除
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
		queryWrapper.in(Setmeal::getId,ids);
		queryWrapper.eq(Setmeal::getStatus,1);

		int count = this.count(queryWrapper);
		if(count > 0){
			//如果不能删除，抛出一个业务异常
			throw new CustomException("套餐正在售卖中，不能删除");
		}

		//如果可以删除，先删除套餐表中的数据---setmeal
		this.removeByIds(ids);

		//delete from setmeal_dish where setmeal_id in (1,2,3)
		LambdaUpdateWrapper<SetmealDish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.in(SetmealDish::getSetmealId,ids);
		//删除关系表中的数据----setmeal_dish
		setmealDishService.remove(lambdaUpdateWrapper);
	}
	/**
	 * 通过id查询套餐信息， 同时还要查询关联表setmeal_dish的菜品信息进行回显。
	 *
	 * @param id 待查询的id
	 */
	@Override
	public SetmealDto getByIdWithDish(Long id) {
		// 根据id查询setmeal表中的基本信息
		Setmeal setmeal = this.getById(id);
		SetmealDto setmealDto = new SetmealDto();
		// 对象拷贝。
		BeanUtils.copyProperties(setmeal, setmealDto);
		// 查询关联表setmeal_dish的菜品信息
		LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SetmealDish::getSetmealId, id);
		List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
		//设置套餐菜品属性
		setmealDto.setSetmealDishes(setmealDishList);
		return setmealDto;
	}
	/**
	 * 更新套餐信息，不仅要更新setmeal基本信息， 还要更新套餐所对应的菜品到setmeal_dish表
	 *
	 * @param setmealDto
	 */
	@Override
	public void updateWithDish(SetmealDto setmealDto) {
		// 保存setmeal表中的基本数据。
		this.updateById(setmealDto);
		// 先删除原来的套餐所对应的菜品数据。
		LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
		setmealDishService.remove(queryWrapper);
		// 更新套餐关联菜品信息。setmeal_dish表。
		// Field 'setmeal_id' doesn't have a default value] with root cause
		// 所以需要处理setmeal_id字段。
		// 先获得套餐所对应的菜品集合。
		List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
		//每一个item为SetmealDish对象。
		setmealDishes = setmealDishes.stream().map((item) -> {
			//设置setmeal_id字段。
			item.setSetmealId(setmealDto.getId());
			return item;
		}).collect(Collectors.toList());

		// 重新保存套餐对应菜品数据
		setmealDishService.saveBatch(setmealDishes);
	}




}
